<div class="layuimini-main">

	<div class="layui-form layuimini-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">权限名称</label>
			<div class="layui-input-block">
				<input type="text" name="name" lay-verify="required" lay-reqtext="权限名称不能为空" placeholder="请填写权限名称" value="" class="layui-input">
				<tip>填写权限的名称。</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">权限code</label>
			<div class="layui-input-block">
				<input type="text" name="code" lay-verify="required" lay-reqtext="权限code不能为空" placeholder="请填写权限code" value=""
				 class="layui-input">
				<tip>填写权限的code。</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">url</label>
			<div class="layui-input-block">
				<input type="text" name="url" l placeholder="请填写url地址" value="" class="layui-input">
				<tip>填写权限的url地址</tip>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">类型</label>
			<div class="layui-input-block">
				<select id="type_select" name="type" lay-verify="required" lay-reqtext="必须选择类型">
					<option value=""></option>
					<option value="1">菜单</option>
					<option value="2">页面</option>
					<option value="3">接口</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" pane="">
			<label class="layui-form-label">是否显示</label>
			<div class="layui-input-block">
				<input type="radio" name="state" value="1" title="显示" checked="">
				<input type="radio" name="state" value="-1" title="不显示">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">排序</label>
			<div class="layui-input-block">
				<input type="number" name="sort" placeholder="请输入排序" value="" class="layui-input">
				<tip>请填写排序。</tip>
			</div>
		</div>
		<input type="hidden" name="parent_pid" />
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">备注信息</label>
			<div class="layui-input-block">
				<textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
	layui.use(['form', 'table', 'hsw'], function() {
		var form = layui.form,
			layer = layui.layer,
			table = layui.table,
			hsw = layui.hsw,
			$ = layui.$;

		// 当前弹出层，防止ID被覆盖
		var parentIndex = layer.index;

		//监听提交
		form.on('submit(saveBtn)', function(data) {
			//console.log(data.field);
			hsw.hsw_post(HSW_API_MANAGE_PEIMISSION_ADD, data.field, function(data) {
					//console.log(data);

					layer.msg('操作成功，即将刷新页面', {
						time: 3000 //
					}, function() {
						hsw.hsw_html_reload(0);
					});

					// 关闭弹出层
					layer.close(index);
					layer.close(parentIndex);

			});
			return false;
		});

		window.addInit = function(data) {
			if(data != null){
				$('input[name="parent_pid"]').attr('value', data.pid);
				
				if (data.type == 1) {
					hsw.hsw_select([{
							"name": "菜单",
							"pid": 1
						}, {
							"name": "页面",
							"pid": 2
						},
						{
							"name": "接口",
							"pid": 3
						}
					], 'type_select');
				} else if (data.type == 2) {
					hsw.hsw_select([{
						"name": "接口",
						"pid": 3
					}], 'type_select');
				}
				
			}else{
				hsw.hsw_select([{
						"name": "菜单",
						"pid": 1
					}, {
						"name": "页面",
						"pid": 2
					},
					{
						"name": "接口",
						"pid": 3
					}
				], 'type_select');
			}
			form.render();
		}

	});
</script>
